package com.taxshare.purchaser.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.mybatis.utils.DozerBeanUtil;
import com.taxshare.purchaser.constant.CodeConstant;
import com.taxshare.purchaser.constant.ResultConstant;
import com.taxshare.purchaser.entity.InvoiceAddressRulesEntity;
import com.taxshare.purchaser.mapper.InvoiceAddressRulesMapper;
import com.taxshare.purchaser.view.dto.InvoiceAddressRulesDTO;
import com.taxshare.response.beans.Response;
import com.taxshare.tools.util.UUIDUitl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: InvoiceAddressRulesService
 * @Description:
 * @author: hjh
 * @date: 2021/11/29 16:23
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
public class InvoiceAddressRulesService extends ServiceImpl<InvoiceAddressRulesMapper, InvoiceAddressRulesEntity> {

    /**
     * 用户手机地址银行账户规则对内容更新
     * @param bean
     * @return
     */
    public Response updateAddressRules(InvoiceAddressRulesDTO bean) {
        if(StrUtil.hasBlank(bean.getUuid())){
            return Response.error(ResultConstant.RET_R0001_CODE, ResultConstant.RET_R0001_MSG);
        }
        if(StrUtil.hasBlank(bean.getAddressNotPassReasons())){
            return Response.error(ResultConstant.RET_R0002_CODE, ResultConstant.RET_R0002_MSG);
        }
        //首次没有buyerRuleId，添加
        InvoiceAddressRulesEntity flg = baseMapper.selectOne(new QueryWrapper<InvoiceAddressRulesEntity>().eq("uuid",bean.getUuid()));
        if(flg==null){
            InvoiceAddressRulesEntity entity = DozerBeanUtil.map(bean, InvoiceAddressRulesEntity.class);
            entity.setAddressRuleId(UUIDUitl.UUID32(false));
            entity.setUuid(bean.getUuid());
            int result = baseMapper.insert(entity);
            if(result!=1){
                return Response.error(ResultConstant.RET_D0001_CODE, ResultConstant.RET_D0001_MSG);
            }
            return Response.success();
        }
        //如果有addressRuleId，内容更新
        UpdateWrapper<InvoiceAddressRulesEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("uuid",bean.getUuid())
                .set("address_not_pass_reasons",bean.getAddressNotPassReasons());
        InvoiceAddressRulesEntity entity = new InvoiceAddressRulesEntity();
        int result = baseMapper.update(entity,updateWrapper);
        if(result!=1){
            return Response.error(ResultConstant.RET_D0002_CODE, ResultConstant.RET_D0002_MSG);
        }
        return Response.success();
    }

    /**
     * 用户手机地址银行账户规则对isOpen更新
     * @param bean
     * @return
     */
    public Response updateAddressIsOpen(InvoiceAddressRulesDTO bean) {
        if(StrUtil.hasBlank(bean.getUuid())){
            return Response.error(ResultConstant.RET_R0001_CODE, ResultConstant.RET_R0001_MSG);
        }
        if(!CodeConstant.RULES_OPEN.equals(bean.getAddressIsOpen())&&(!CodeConstant.RULES_NOT_OPEN.equals(bean.getAddressIsOpen()))){
            return Response.error(ResultConstant.RET_R0001_CODE, ResultConstant.RET_R0001_MSG);
        }
        InvoiceAddressRulesEntity flg = baseMapper.selectOne(new QueryWrapper<InvoiceAddressRulesEntity>().eq("uuid",bean.getUuid()));
        //首次添加
        if(flg==null){
            InvoiceAddressRulesEntity entity = new InvoiceAddressRulesEntity();
            entity.setAddressRuleId(UUIDUitl.UUID32(false));
            entity.setUuid(bean.getUuid());
            entity.setAddressIsOpen(bean.getAddressIsOpen());
            int result = baseMapper.insert(entity);
            if(result!=1){
                return Response.error(ResultConstant.RET_D0001_CODE, ResultConstant.RET_D0001_MSG);
            }
            return Response.success();
        }
        //非首次更新
        UpdateWrapper<InvoiceAddressRulesEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("uuid",bean.getUuid())
                .set("address_is_open",bean.getAddressIsOpen());
        if (CodeConstant.COMM_CODE_0.equals(bean.getAddressIsOpen())) {
            updateWrapper.set("force_flag", bean.getAddressIsOpen());
        }
        InvoiceAddressRulesEntity entity = new InvoiceAddressRulesEntity();
        int result = baseMapper.update(entity,updateWrapper);
        if(result!=1){
            return Response.error(ResultConstant.RET_D0002_CODE, ResultConstant.RET_D0002_MSG);
        }
        return Response.success();
    }

    /**
     * 用户手机地址银行账户规则对强制开关更新
     * @param bean
     * @return
     */
    public Response updateAddressForceFlag(InvoiceAddressRulesDTO bean) {
        if(StrUtil.hasBlank(bean.getUuid())){
            return Response.error(ResultConstant.RET_R0001_CODE, ResultConstant.RET_R0001_MSG);
        }
        if(!CodeConstant.RULES_NOT_OPEN.equals(bean.getForceFlag())&&(!CodeConstant.RULES_OPEN.equals(bean.getForceFlag()))){
            return Response.error(ResultConstant.RET_R0001_CODE, ResultConstant.RET_R0001_MSG);
        }
        InvoiceAddressRulesEntity flg = baseMapper.selectOne(new QueryWrapper<InvoiceAddressRulesEntity>().eq("uuid",bean.getUuid()));
        if(flg!=null&& CodeConstant.COMM_CODE_0.equals(flg.getAddressIsOpen())){
            return Response.error(ResultConstant.RET_P0018_CODE, ResultConstant.RET_P0018_MSG);
        }
        UpdateWrapper<InvoiceAddressRulesEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("uuid",bean.getUuid())
                .set("force_flag",bean.getForceFlag());
        InvoiceAddressRulesEntity entity = new InvoiceAddressRulesEntity();
        int result = baseMapper.update(entity,updateWrapper);
        if(result!=1){
            return Response.error(ResultConstant.RET_D0002_CODE, ResultConstant.RET_D0002_MSG);
        }
        return Response.success();
    }
}
